package everyday;

import cn.hutool.core.bean.BeanUtil;
import entity.UserDto;
import entity.UserReq;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

/**
 * 测试 hutool-BeanUtil工具类
 *
 * @author gaojie
 * @date 2024-03-21
 */
public class HuToolBeanUtilTest {

    /**
     * 测试 copyProperties，在copy时，如果属性类型不一致，能否 copy
     * 结论：也能转换
     */
    @Test
    void test01() {
        final UserDto userDto = new UserDto();
        userDto.setName("AA");
        userDto.setAge(10);
        userDto.setAmount(new BigDecimal("12.345"));
        System.out.println(BeanUtil.copyProperties(userDto, UserReq.class));

        final UserReq userReq = new UserReq();
        userReq.setName("BB");
        userReq.setAge("11.2");
        userReq.setAmount("99.888");
        System.out.println(BeanUtil.copyProperties(userReq, UserDto.class));
    }

    /**
     * 测试 copyProperties，在copy时，如果源对象的属性为null，会不会把目标属性存在的值覆盖
     * 测试结果：都会被覆盖掉
     */
    @Test
    void test02() {
        UserReq source = new UserReq();
        source.setName("AA");

        UserReq target = new UserReq();
        target.setName("BB");
        target.setAge("11.2");
        target.setAmount("99.888");

        BeanUtil.copyProperties(source, target);
        System.out.println(target);
    }
}
